package com.haima.sage.bigdata.api.frame.interpreter.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.haima.sage.bigdata.api.frame.interpreter.ValueService;

public class DateValueService implements ValueService {
	private String field;

	private SimpleDateFormat sdf;

	public DateValueService(String field, String format) {
		this.field = field;
		this.sdf = new SimpleDateFormat(format);
	}

	@Override
	public String value(Map<?, ?> event) {
		Object value = event.get(field);
		if (value == null) {
			return defaultValue();
		}
		if (value instanceof Date) {
			return sdf.format((Date) value);
		}
		return defaultValue();
	}

	private String defaultValue() {
		return sdf.format(new Date());
	}
}
